<template>
  <div>
    <!-- 1.顶部栏 -->
    <div class="header">
      <van-image
        width="60"
        height="60"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <h2>
        {{ userInfo.name }}<br />
        <van-tag color="#fff" text-color="#009afb">{{
          userInfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- 2.操作链接 -->
    <van-row gutter="20">
      <van-col span="8"
        ><van-icon name="newspaper-o" size="25" color="#7af" />
        <span>我的作品</span>
      </van-col>
      <van-col span="8"
        ><van-icon name="star-o" size="25" color="#f00" />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8"
        ><van-icon name="tosend" size="25" color="#fa0" />
        <span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- 3.cell列表 -->
    <van-cell-group class="user-group">
      <van-cell
        icon="edit"
        title="编辑资料"
        is-link
        @click="$router.push('/edit')"
      />
      <van-cell
        icon="chat-o"
        title="小智同学"
        is-link
        @click="$router.push('/student')"
      />
      <van-cell icon="setting-o" title="系统设置" is-link />
      <van-cell
        icon="warning-o"
        title="退出登录"
        is-link
        @click="logoutClick"
      />
    </van-cell-group>
  </div>
</template>

<script>
// 导入网络中间层
import { getUserInfo } from './user.js'
// 导入token工具包
import { removeToken } from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      userInfo: {}
    }
  },
  created () {
    // 1.请求个人信息
    getUserInfo().then(res => {
      console.log(res)
      // 1.1绑定数据
      this.userInfo = res.data.data
      // 1.2更新到vuex
      this.$store.commit('updateUserInfo', res.data.data)
    })
  },
  methods: {
    logoutClick () {
      this.$dialog
        .confirm({
          title: '提示',
          message: '确定要退出吗？'
        })
        .then(() => {
          // 点击确认：退出登录
          // 清楚token
          removeToken()
          // 跳入登录界面
          this.$router.push('/login')
        })
    }
  }
}
</script>

<style lang="less">
// 顶部栏
.header {
  height: 100px;
  background-color: #009afb;
  // 伸缩盒子
  display: flex;
  // 垂直居中
  align-items: center;
  .van-image {
    padding: 0px 10px;
  }
  h2 {
    font-size: 18px;
    color: #fff;
    font-weight: 400;
  }
}
.van-row {
  .van-col {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    span {
      margin-top: 8px;
      display: block;
      font-size: 12px;
    }
  }
}
</style>
